package top.colordreams.tenant.sping.starter;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import top.colordreams.tenant.TenantContext;

/**
 * 动态租户助手
 *
 * @author 秋辞未寒
 */
@Component
public class DynamicTenantHelper implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        DynamicTenantHelper.applicationContext = applicationContext;
    }

    /**
     * 获取动态租户上下文
     *
     * @return 动态租户上下文
     */
    public static TenantContext getTenantContext() {
        return applicationContext.getBean(TenantContext.class);
    }

    /**
     * 获取默认租户ID
     *
     * @return 默认租户ID
     */
    public static String getDefaultTenantId() {
        return getTenantContext().getDefaultTenantId();
    }

    /**
     * 获取当前租户ID
     *
     * @return 当前租户ID
     */
    public static String getCurrentTenantId() {
        return getTenantContext().getCurrentTenantId();
    }

    /**
     * 默认租户是否为根租户
     *
     * @return 默认租户是否为根租户
     */
    public static boolean defaultTenantIsRoot() {
        return getTenantContext().defaultTenantIsRoot();
    }

    /**
     * 获取根租户ID
     *
     * @return 根租户ID
     */
    public static String getRootTenantId() {
        return getTenantContext().getRootTenantId();
    }

    /**
     * 判断租户ID是否为根租户
     *
     * @param tenantId 租户ID
     * @return 是否为根租户
     */
    public static boolean isRootTenant(String tenantId) {
        return getTenantContext().isRootTenant(tenantId);
    }

    /**
     * 判断当前租户ID是否为根租户
     *
     * @return 是否为根租户
     */
    public static boolean isRootTenant() {
        return getTenantContext().isRootTenant();
    }

    /**
     * 判断租户ID是否为默认租户
     *
     * @param tenantId 租户ID
     * @return 是否为默认租户
     */
    public static boolean isDefaultTenant(String tenantId) {
        return getTenantContext().isDefaultTenant(tenantId);
    }

    /**
     * 判断当前租户ID是否为默认租户
     *
     * @return 是否为默认租户
     */
    public static boolean isDefaultTenant() {
        return getTenantContext().isDefaultTenant();
    }

}
